package com.grf.rocketmq;

import java.util.ArrayList;
import java.util.List;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import lombok.SneakyThrows;

/**
 * @author : guorf
 * @version : v1.0
 * @description : TODO 批量消息
 * @createTime : 2023/9/13 15:20
 */
public class BatchProducer {
    /**
     * @description TODO 批量发送消息
     * @param args
     * 在对吞吐率有一定要求的情况下，Apache RocketMQ可以将一些消息聚成一批以后进行发送，可以增加吞吐率，并减少API和网络调用次数
     **/
    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = ProducerUtil.start();

        //If you just send messages of no more than 1MiB at a time, it is easy to use batch
        //Messages of the same batch should have: same topic, same waitStoreMsgOK and no schedule support
        String topic = ProducerUtil.TOPIC;
        List<Message> messages = new ArrayList<>();
        messages.add(new Message(topic, "Tag", "OrderID001", "Hello world 0".getBytes()));
        messages.add(new Message(topic, "Tag", "OrderID002", "Hello world 1".getBytes()));
        messages.add(new Message(topic, "Tag", "OrderID003", "Hello world 2".getBytes()));
        producer.send(messages);
        ProducerUtil.shutdown(producer);
    }
}
